﻿using Ec_System.Models.Web;
using Ilongshi.Oversea.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Ec_System.Service.Web
{
    public class ItemOrderService
    {
        /// <summary>
        /// 根据LoginID获取前5条订单
        /// </summary>
        /// <param name="loginID"></param>
        /// <returns></returns>
        public List<ItemOrderData> GetTop5OrderByLoginID(string loginID)
        {
            var cmd = DataCommandManager.GetDataCommand("GetTop5OrderByLoginID");
            cmd.SetParameterValue("@LoginID", loginID);

            return cmd.ExecuteEntityList<ItemOrderData>();
        }

        /// <summary>
        /// 根据LoginID查询指定的订单(带分页)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ItemOrderResponseData GetOrderListWithPageByLoginID(ItemOrderRequestData request)
        {
            var cmd = DataCommandManager.GetDataCommand("GetOrderListWithPageByLoginID");
            cmd.SetParameterValue("@LoginId", request.loginID);
            cmd.SetParameterValue("@PageIndex", request.PageIndex);
            cmd.SetParameterValue("@PageSize", request.PageSize);

            ItemOrderResponseData result = new ItemOrderResponseData()
            {
                PageIndex = request.PageIndex,
                PageSize = request.PageSize,                
                Result = cmd.ExecuteEntityList<ItemOrderData>(),
                TotalCount = cmd.GetParameterValue<int>("@TotalCount")
            };

            return result;
        }

        /// <summary>
        /// 根据订单列表的xml查询明细
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public List<ItemOrderDetailsData> GetOrderDetailByOrderID(string xml)
        {
            var cmd = DataCommandManager.GetDataCommand("GetOrderDetailByOrderID");
            cmd.SetParameterValue("@OrderXml", xml);

            return cmd.ExecuteEntityList<ItemOrderDetailsData>();
        }

        /// <summary>
        /// 退货
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="orderDetailId"></param>
        /// <param name="vendorNumber"></param>
        /// <param name="reason"></param>
        /// <returns></returns>
        public int ReturnOrder(string orderId, string orderDetailId, string vendorNumber, string reason)
        {
            var cmd = DataCommandManager.GetDataCommand("ReturnOrder");
            cmd.SetParameterValue("@OrderId", orderId);
            cmd.SetParameterValue("@OrderDetailId", orderDetailId);
            cmd.SetParameterValue("@VendorNumber", vendorNumber);
            cmd.SetParameterValue("@Reason", reason);

            return cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// 根据OrderID获取指定的订单信息
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public ItemOrderData GetOrderInfoByOrderID(string orderID)
        {
            var cmd = DataCommandManager.GetDataCommand("GetOrderInfoByOrderID");
            cmd.SetParameterValue("@OrderId", orderID);

            return cmd.ExecuteEntity<ItemOrderData>();
        }

        /// <summary>
        /// 根据OrderID 和 ProductID 获取指定的订单明细表数据
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public ItemOrderDetailsData GetOrderDetailByOrderIDAndPID(string orderId, string productID)
        {
            var cmd = DataCommandManager.GetDataCommand("GetOrderDetailByOrderIDAndPID");
            cmd.SetParameterValue("@OrderId", orderId);
            cmd.SetParameterValue("@ProductID", productID);

            return cmd.ExecuteEntity<ItemOrderDetailsData>();
        }
    }
}
